var session;
var socket = io.connect(); // I'm assuming that you know how to use socket.io
socket.on('messageReceived', onVideoMessageReceived);
socket.on('onMessage', function(message) {
    session.receiveMessage(message);
});

function call(isInitiator) {
    var config = {
        isInitiator: isInitiator,
        turn: {
            host: turn.url,
            username: turn.username,
            password: turn.credential
        },
        streams: {
            audio: true,
            video: true
        }
    };
    session = new cordova.plugins.phonertc.Session(config);
    cordova.plugins.phonertc.setVideoView({
        container: document.getElementById('videoContainer'),
        local: {
            position: [0, 0],
            size: [100, 100]
        }
    });
    session.on('sendMessage', function(data) {
        socket.emit('sendMessage', {
            type: 'phonertc_handshake',
            data: JSON.stringify(data)
        });
    });
    session.call();
}

function onVideoMessageReceived(message) {
    switch (message.type) {
        case 'call':
            $('#answer').show();
            break;
        case 'answer':
            call(true);
            break;
        case 'phonertc_handshake':
            // run this only once during the start of a call
            session.receiveMessage(JSON.parse(message.data));
            break;
    }
}